/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dnbW.uiW.queryW.modelW;

import dnbW.dbW.PathExpression;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author zhouyong
 */
public class PathExistenceNodeObject extends ConditionNodeLeafObject {

    private PathExpression pathExpression;

    public PathExistenceNodeObject(DefaultMutableTreeNode node, PathExpression pathExpression) {
        //super(node);
        this.pathExpression = pathExpression;
    }

    //@Override
    public String getXPath(PathExpression outputPath) {
        // note: this implementation does not support descendant axis
        // and does not support cross database join
        if (!outputPath.getDataSource().equals(this.getPathExpression().getDataSource())) {
            return null;
        }

        StringBuilder result = new StringBuilder();
        List<DefaultMutableTreeNode> list1 = outputPath.getFullPath();
        List<DefaultMutableTreeNode> list2 = this.getPathExpression().getFullPath();

        int lcp = PathExpression.lcp(outputPath, this.getPathExpression());

        for (int i = 0; i <= lcp; i++) {
            result.append("/");
            result.append(list1.get(i));
        }
        result.append("[");
        for (int i = lcp + 1; i < list2.size(); i++) {
            if (i != (lcp + 1)) {
                result.append("/");
            }
            result.append(list2.get(i));
        }
        if (this.getPathExpression().getFromIndex() != -1) {
            result.append("[");
            result.append(this.getPathExpression().getFromIndex());
            result.append("]");
        }
        result.append("]");

        for (int i = lcp + 1; i < list1.size(); i++) {
            result.append("/" + list1.get(i));
        }

        return result.toString();
    }

    public PathExpression getPathExpression() {
        return pathExpression;
    }

    //@Override
    public String getXPath(PathExpression outputPath, ConditionCluster CurrentConditionCluster) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    //@Override
    public String getXQuery(PathExpression outputPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
